//
//  UIImage+scale.swift
//  图片选择器
//
//  Created by danli on 15/12/30.
//  Copyright © 2015年 danli. All rights reserved.
//

import UIKit

extension UIImage
{
    func reduceImage() -> UIImage
    {
        let scaleWidth = CGFloat(300)
        let width = self.size.width
        if width < scaleWidth
        {
            return self
        }
        
        //缩小图片
        let scaleHeigth = size.height/size.width * scaleWidth
        let scaleSize = CGSize(width: scaleWidth, height: scaleHeigth)
        
        // 返回缩小的图片
        UIGraphicsBeginImageContext(scaleSize)
        // 绘图
        drawInRect(CGRect(origin: CGPointZero, size: scaleSize))
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return newImage
        
    }
    
}

